package org.jeecg.modules.qyxx.config;

import jodd.util.StringUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author chensherun on 2022/1/12.
 * @version 1.0
 */
@Data
@Component
@ConfigurationProperties(prefix = "area-config")
public class AreaConfigProperties {

    private Map<String, AreaNetCode> areaNetCodeMap;

    @Data
    public static class AreaNetCode {
        private String areaName;
        private String netCode;
    }

    @Data
    public static class AreaCodeName {
        private String code;
        private String value;
    }

    public List<AreaCodeName> getAreasList() {
        List<AreaCodeName> list = new ArrayList<>();
        areaNetCodeMap.forEach((k, v) -> {
            if (!k.startsWith("4403")) {
                AreaCodeName area = new AreaCodeName();
                area.setCode(k);
                area.setValue(v.getAreaName());
                list.add(area);
            }
        });
        return list;
    }

    public String getAreaCityCode(String cityName) {
        if (StringUtil.isBlank(cityName)){
            return null;
        }
        Set<Map.Entry<String, AreaNetCode>> entries = areaNetCodeMap.entrySet();
        for (Map.Entry<String, AreaNetCode> entry : entries) {
            AreaNetCode entryValue = entry.getValue();
            String name = entryValue.getAreaName();
            if (name.equals(cityName)){
                return entry.getKey();
            }
        }
        return null;
    }
}
